﻿using System;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.Services;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmThayDoiCoSo : frmDetail
    {
        public Guid IdPhieu;
        private readonly IService<ThayDoiCoSo> _thayDoiCoSoService;

        public frmThayDoiCoSo()
        {
            InitializeComponent();
            _thayDoiCoSoService = Program.Container.Resolve<IService<ThayDoiCoSo>>();
            Text = @"Thêm mới Thay đổi cơ sở";
            dtNgay.Focus();
        }

        public frmThayDoiCoSo(Guid id)
        {
            InitializeComponent();
            _thayDoiCoSoService = Program.Container.Resolve<IService<ThayDoiCoSo>>();
            Text = @"Sửa Thay đổi cơ sở";
            dtNgay.Focus();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _thayDoiCoSoService.Detail(id);
            if (item != null)
            {
                dtNgay.Value = item.Ngay.Value;
                txtGhiChu.Text = item.GhiChu;
                txtNoiDUng.Text = item.NoiDungThayDoi;
            }
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new ThayDoiCoSo
                {
                    Id = Guid.NewGuid(),
                    Ngay = dtNgay.Value.Date,
                    NoiDungThayDoi = txtNoiDUng.Text,
                    GhiChu = txtGhiChu.Text,
                    PhieuDieuTraCoSoId = IdPhieu,
                    Created = DateTime.Now,
                    CreatedBy = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy = Constants.CurrentUser
                };
                _thayDoiCoSoService.Create(model);

            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _thayDoiCoSoService.Detail(guid);
            if (item != null)
            {
                item.Ngay = dtNgay.Value.Date;
                item.NoiDungThayDoi = txtNoiDUng.Text;
                item.GhiChu = txtGhiChu.Text;
                //item.PhieuDieuTraCoSoId = IdPhieu;
                item.Modified = DateTime.Now;
                item.ModifiedBy = Constants.CurrentUser;
                _thayDoiCoSoService.Update(item);
            }
        }

        #endregion
    }
}
